//OmusicLyrics Class

function OmusicLyrics()
{
    this.lyricsArray = null;
    this.t_data = '';
    this.a_data = '';
    this.title='';
    this.author='';
    this.lyricsAddr = '';
    this.req = null;
    this.delay = 0;
}

OmusicLyrics.prototype =
    {
	//public
	getDisplay : function()
	{
	    return document.getElementById('oplayer-lyrics');
	},
	
	localLyrics : function(title, author, path)
	{
	    this.lyricsArray = null;
	    this.lyricsArray = new Array();

	    var lyricsPath = path.replace(/\.mp3$/, '.lrc');
	    var lyricsFile = FileIO.open(lyricsPath);

	    if(lyricsFile && lyricsFile.exists() && lyricsFile.isFile())
	    {
		var lyctext = title;
		if(author && author != '')lyctext = lyctext + ' - ' + author;
		this.lyricsArray[0]= lyctext;

		var str = FileIO.read(lyricsFile, 'gb2312');
		this.lyricsAddr = FileIO.path(lyricsFile);
		this.process(str);
	    }
	    else
	    {
		this.prepareLyrics(title, author);
	    }
	},
	
	prepareLyrics : function(title, author)
	{
	    this.lyricsArray = null;
	    this.lyricsArray = new Array();
	    
	    var lyctext = title;
	    if(author && author != '')lyctext = lyctext + ' - ' + author;
	    this.lyricsArray[0]= lyctext;
	    
	    var uniConv = Components.classes['@mozilla.org/intl/scriptableunicodeconverter'].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
	    uniConv.charset = 'gb2312';
	    this.t_data = escape(uniConv.ConvertFromUnicode(title));
	    this.a_data = escape(uniConv.ConvertFromUnicode(author));
	    
	    
	    
	    var preaddr = 'onlinect';
	    var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
	    prefs = prefs.getBranch("extensions.omusic.");
	    
	    preaddr = prefs.getCharPref('preaddr');		
	    
	    var add1 = "http://" + preaddr + ".ttplayer.com/player/index.php?s=1&checkmp3="+this.t_data+'|'+this.a_data;
	    
	    this.req = new XMLHttpRequest();
	    this.req.open('GET', add1, true);
	    this.req.onreadystatechange = this.wrapper(this, 'preprocess');
	    this.req.send(null);		
	},
	
	incDelay : function(n)
	{
	    var i = 1;
	    if(n != undefined)i = n;
	    this.delay = this.delay + i;
	},
	
	decDelay : function(n)
	{
	    var i = 1;
	    if(n != undefined)i = n;
	    this.delay = this.delay - i;		
	},
	
	setDelay : function(t)
	{
	    this.delay = parseInt(t);
	},
	
	updateDisplay : function(time)
	{
	    var tt = time;
	    if(tt == 'clear')
	    {
		this.getDisplay().setAttribute('value', '');
		return;
	    }
	    if(this.lyricsArray)
	    {
		var tt2 = tt + this.delay;
		while(!this.lyricsArray[tt2]&&tt2 > 0)tt2--;
		if(this.lyricsArray[tt2])this.getDisplay().setAttribute('value', this.lyricsArray[tt2]);
	    }
	},
	
	//private
	
	preprocess : function(str)
	{
	    var preaddr = 'onlinect';
	    var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
	    prefs = prefs.getBranch("extensions.omusic.");
	    
	    preaddr = prefs.getCharPref('preaddr');
	    
	    ress = /http:\/\/.*\.ttplayer\.com\/player\/getlrc\.php.*(&e=[^\'\"]*)/.exec(str);
	    
	    var addr = 'http://' + preaddr + '.ttplayer.com/player/getlrc.php?t='+this.t_data+'&a='+this.a_data+ress[1];
	    this.lyricsAddr = addr;
	    
	    this.req = new XMLHttpRequest();
	    this.req.open('GET', addr, true);
	    this.req.onreadystatechange = this.wrapper(this); 
	    this.req.send(null); 
	},
	
	process : function(str)
	{
	    var abc = str;

	    var reg5 = /\[offset:(\d+)\]/;
	    var off;
	    if((off = reg5.exec(abc)))
	    {
		this.delay = Math.round(Math.round(off[1])/1000);
	    }
	    
	    var reg = /(?:\[[\d.:]+\][\s\r\n]*)+[^\[\]]*/g;
	    var result;
	    var firstRound = new Array();
	    var i = 0;
	    while((result = reg.exec(str)) != undefined)
	    {
		firstRound[i] = result;
		i++;
	    }
	    
	    

	    var reg2 = /\[([\d:.]+)\]/g;
	    var reg3 = /(?:\[.*\][\n\r\s]*)+([^\[\]]+)/;
	    var reg4 = /(\d+):(\d+)(?:\.(\d+))?/;
	    
	    var len = firstRound.length;

	    for(i = 0; i<len; i++)
	    {
		var strr = firstRound[i];
		var txt = reg3.exec(strr);
		while((result = reg2.exec(strr)) != undefined)
		{
		    var ss = reg4.exec(result[1]);
		    var tt = Math.round(ss[2])+ Math.round(ss[1])* 60;
		    if(ss[3] != undefined)tt = tt + Math.round(ss[3]>50 ? 1 : 0);
		    var txtt = txt[1];
		    txtt = txtt.replace(/\r/g, '');
		    txtt = txtt.replace(/\n/g, '');
		    this.lyricsArray[tt]= txtt;
		}
	    }
	},
	
	wrapper : function(obj, func)
	{
	    return function()
	    {
		return obj['callback'](func);
	    }
	},
	
	callback : function(func)
	{
  	    if (this.req.readyState == 4)
  	    {
   		if(this.req.status == 200)
   		{
   		    if(func == undefined)
     			this.process(this.req.responseText);
     		    else
     			this[func](this.req.responseText);
     		}
	    }
	}
    };
